package export.huodong.mysterious
{
	import com.dusk.util.ResUtil;
	import com.dusk.util.TextUtil;
	import com.game.data.constant.ConfigConst;
	import com.game.data.vo.EquipmentVO;
	import com.game.event.GameEvent;
	import com.game.manager.EventManager;
	import com.game.manager.NotificationManager;
	import com.game.module.backpack.view.PackThings;
	
	import config.Config;
	
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class SingleShop extends Sprite
	{
		
		public function SingleShop()
		{
			super();
			TextUtil.redrawTxtOld(this, ["txt_price", "txt_name"]);
			this.gc = Config.getIns();
			this.addEventListener(Event.ADDED_TO_STAGE, this.added, false, 0, true);
			this.addEventListener(Event.REMOVED_FROM_STAGE, this.removed, false, 0, true);
			this.maskmc = ResUtil.getNewObj("singleShopMask") as Sprite;
			this.maskmc.name = "maskmc";
			this.addChild(this.maskmc);
			var _loc1_:TextFormat;
			txt_name.embedFonts = true;
			_loc1_ = txt_name.defaultTextFormat;
			_loc1_.font = ConfigConst.GAME_CONFIG_FONT;
			txt_name.defaultTextFormat = _loc1_;
			txt_name.setTextFormat(_loc1_);
			
			txt_price.embedFonts = true;
			_loc1_ = txt_price.defaultTextFormat;
			_loc1_.font = ConfigConst.GAME_CONFIG_FONT;
			txt_price.defaultTextFormat = _loc1_;
			txt_price.setTextFormat(_loc1_);
		}
		
		public var buy_btn:SimpleButton;
		public var txt_price:TextField;
		public var txt_name:TextField;
		private var packThings:PackThings;
		private var gc:Config;
		private var maskmc:Sprite;
		private var mynum:uint;
		private var thingsnum:int;
		private var price:int;
		
		public function setTheShopNum(param1:uint):void
		{
			if (this.getChildByName("packThings"))
			{
				this.removeChild(this.getChildByName("packThings"));
				this.packThings = null;
			}
			if (param1 == 1)
			{
				this.removedMask();
			}
			this.setMynum(param1);
			var _loc2_:EquipmentVO = this.findThingBySign(param1);
			if (_loc2_)
			{
				if (this.getThingsNum() > 1)
				{
					this.txt_name.text = _loc2_.name + "x" + this.getThingsNum();
				}
				else
				{
					this.txt_name.text = _loc2_.name;
				}
				this.txt_price.text = this.getPrice() + "仙魂币";
				if (this.packThings == null)
				{
					this.packThings = new PackThings();
					this.packThings.name = "packThings";
					this.packThings.x = 23;
					this.packThings.y = 18;
					this.addChild(this.packThings);
					if (this.getChildByName("maskmc") != null)
					{
						if (this.getChildIndex(this.packThings) > this.getChildIndex(this.maskmc))
						{
							this.swapChildren(this.packThings, this.maskmc);
						}
					}
				}
				this.packThings.setEquipVO(_loc2_, this.gc.playerData.getControlPlayer(), 2);
			}
		}
		
		public function removedMask():void
		{
			if (this.getChildByName("maskmc") != null)
			{
				this.removeChild(this.maskmc);
			}
		}
		
		public function addMask():void
		{
			if (this.getChildByName("maskmc") == null)
			{
				this.addChild(this.maskmc);
			}
		}
		
		public function setThingsNum(param1:int):void
		{
			this.thingsnum = param1;
		}
		
		public function getThingsNum():int
		{
			return this.thingsnum;
		}
		
		public function setPrice(param1:int):void
		{
			this.price = param1 / 100;
		}
		
		public function getPrice():int
		{
			return this.price;
		}
		
		public function setMynum(param1:int):void
		{
			this.mynum = param1;
		}
		
		public function getMynum():int
		{
			return this.mynum;
		}
		
		private function findThingBySign(param1:uint):EquipmentVO
		{
			var _loc2_:Array = null;
			var _loc4_:int = 0;
			if (param1 == 1)
			{
				_loc2_ = ["wpsc", "wpxt", "wptm"];
				_loc4_ = Math.round(Math.random() * 2);
				this.setPrice(200);
				this.setThingsNum(15);
				return this.gc.allEquip.findByName(_loc2_[_loc4_]);
			}
			if (param1 == 2)
			{
				_loc2_ = ["wpsc", "wpxt", "wptm"];
				_loc4_ = Math.round(Math.random() * 2);
				this.setPrice(500);
				this.setThingsNum(20);
				return this.gc.allEquip.findByName(_loc2_[_loc4_]);
			}
			if (param1 == 3)
			{
				_loc2_ = ["wpsc", "wpxt", "wptm"];
				_loc4_ = Math.round(Math.random() * 2);
				this.setPrice(1000);
				this.setThingsNum(35);
				if (Math.random() <= 0.001)
				{
					this.setThingsNum(500);
				}
				return this.gc.allEquip.findByName(_loc2_[_loc4_]);
			}
			if (param1 == 4)
			{
				_loc2_ = ["cwjnxld", "cwzzxld", "wwdgl", "lssp_1", "lssp_2", "wpqhs1", "mfs3", "gjs3", "wpqhs2", "yll", "ttlp", "rls", "tss", "yhs"];
				_loc4_ = Math.round(Math.random() * (_loc2_.length - 1));
				this.setPrice(3000);
				this.setThingsNum(1);
				return this.gc.allEquip.findByName(_loc2_[_loc4_]);
			}
			if (param1 == 5)
			{
				if (Math.random() <= 0.9)
				{
					_loc2_ = ["wpqhs5", "bmd", "jbc", "szj", "yp", "ml", "sz", "zz", "gz", "kly3", "kly4", "jljzzs", "jlczzs", "jlgzzs", "plzzzs", "plpzzs", "qlgzzs", "qljzzs", "ylkzzs", "ylfzzs", "lysjns2", "jspjns2"];
					_loc4_ = Math.round(Math.random() * (_loc2_.length - 1));
					this.setPrice(8000);
				}
				else if (this.gc.playerData.level >= 70)
				{
					_loc2_ = ["wpxty", "wpzty", "phhlzzs", "zhhzzzs", "bxhyzzs", "lssp_6", "lssp_7", "lssp_8", "lssp_9", "gjrls", "gjyhs", "gjtss", "_ssggjtg"];
					_loc4_ = Math.round(Math.random() * (_loc2_.length - 1));
					this.setPrice(11000);
				}
				else
				{
					_loc2_ = ["wpxt", "wptm", "wpsc", "lssp_4", "lssp_5", "lssp_6", "lssp_7", "gjrls", "gjyhs", "gjtss"];
					_loc4_ = Math.round(Math.random() * (_loc2_.length - 1));
					this.setPrice(10000);
				}
				if (_loc2_[_loc4_] == "wpxt" || _loc2_[_loc4_] == "wptm" || _loc2_[_loc4_] == "wpsc")
				{
					this.setThingsNum(300);
				}
				else
				{
					setThingsNum(1);
				}
				return this.gc.allEquip.findByName(_loc2_[_loc4_]);
			}
			return null;
		}
		
		private function added(param1:Event):void
		{
			this.buy_btn.addEventListener(MouseEvent.CLICK, this.buyClick);
		}
		
		private function removed(param1:Event):void
		{
			this.buy_btn.removeEventListener(MouseEvent.CLICK, this.buyClick);
		}
		
		private function buyClick(param1:MouseEvent):void
		{
			var _loc2_:EquipmentVO = null;
			var parent:MysteriousShop = (this.parent as MysteriousShop);
			if (int(parent.txttimes.text) > 0)
			{
				if (parent.getTotalCoin() >= this.getPrice())
				{
					this.gc.playerData.removeEquipFormBag("xhb", 2, this.getPrice());
				}
				parent.txttimes.text = (int(parent.txttimes.text) - 1).toString();
				if (this.packThings.getEquipIcon().getEquipVO().type == "zbtx")
				{
					_loc2_ = this.packThings.getEquipIcon().getEquipVO();
					this.gc.playerData.bagEquipList.push(_loc2_);
				}
				else
				{
					this.gc.putQhsInBackPack(this.gc.playerData, this.packThings.getEquipIcon().getEquipVO().equipId, this.getThingsNum());
				}
				parent.lastBuy = this.getMynum();
				EventManager.getIns().dispatchEvent("MysteriousShopBuySuccess");
				NotificationManager.alert("购买成功");
			}
			else
			{
				NotificationManager.alert("次数不够");
			}
		}
	}
}
